package com.roc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.dao.ITerminalDao;
import com.roc.dao.ITerminalSnBatchDao;
import com.roc.pojo.Operators;
import com.roc.pojo.ProfitModule;
import com.roc.pojo.TPosAgent;
import com.roc.pojo.TPosInfo;
import com.roc.pojo.YjPosAgent;
import com.roc.pojo.YjPosBatch;
import com.roc.pojo.YjPosBatchInfo;
import com.roc.pojo.YjPosInfo;
import com.roc.service.ITerminalService;
import com.roc.service.ITerminalSnBatchSerice;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.Utils;
import com.roc.util.constant.ConfigMerinfoUtil;
import com.roc.util.constant.Constant;
import com.roc.util.constant.SysLogType;
import com.roc.util.constant.Terminal;

@Service
@Transactional
@SuppressWarnings("all")
public class TerminalSnBatchSericeImpl implements ITerminalSnBatchSerice{

	static Logger logger=LogManager.getLogger(TerminalSnBatchSericeImpl.class);
	
	@Autowired
	private ITerminalSnBatchDao terminalSnBatchDao;
	
	@Autowired
	private IBaseDao baseDao;
	
	@Autowired
	private ISQLBaseDao sqlBaseDao;
	
	@Autowired
	private ITerminalDao terminalDao;
	
	@Override
	@Transactional(rollbackFor = { ServiceException.class })
	public List<YjPosInfo> batchSaveSeriaNumInfo(List<YjPosInfo> posinfo,
			YjPosInfo pos, Operators operators) throws ServiceException {
		YjPosBatchInfo bean=null;
		YjPosBatch batchBean = new YjPosBatch() ;
		List<YjPosInfo> importedList=new ArrayList<YjPosInfo>();
		int total=0;
		String beginSn = ""; // 开始号段
		String endSn = ""; // 结束号段
		try {
			if (null != posinfo && posinfo.size() > 0) {
				beginSn = Utils.changeCharUpperCase(posinfo.get(0).getSerialNum());
				endSn = Utils.changeCharUpperCase(posinfo.get(0).getSerialNum());
				int nums = posinfo.size();
				// 如果导入sn的号段长度大于1,则 结束号段 取最后一个sn
				if ( nums > 1 ){
					endSn = Utils.changeCharUpperCase(posinfo.get( (nums - 1 )).getSerialNum()); 
				}
				// 生成批次号
				String snBatchNo = Constance.getSnBatchNo();
				// 插入批次表
				batchBean.setBatchNo(snBatchNo); // 批次号(日期 + _ + 随机数)
				batchBean.setBeginSerialNum(beginSn); // 开始sn字段
				batchBean.setEndSerialNum(endSn); // 结束sn字段
				batchBean.setNums(nums); // 总数量
			//	batchBean.setRemark("");; // 备注
				batchBean.setOrgCode(pos.getOrgCode()); // 机构代码
				batchBean.setOperatorNo(operators.getAccountNo());; // 操作账号
				batchBean.setOperatorId(operators.getId()); // 操作id
				batchBean.setAgentId(pos.getAgentId());; // 代理商id
				batchBean.setDtCreate(new Date()); // 创建时间
				batchBean.setDtLastmod(new Date()); // 更新时间
				batchBean.setPosBrand(pos.getPosBrand()); // 品牌
				
				baseDao.editSave(batchBean);

				for (YjPosInfo yjPosInfo : posinfo) {
					bean=new YjPosBatchInfo();
				
					bean.setBatchNo(snBatchNo);; // 批次号(日期 + _ + 随机数)
					bean.setSerialNum(Utils.changeCharUpperCase(yjPosInfo.getSerialNum()));// 终端sn序列号
					bean.setPosBrand(pos.getPosBrand()); // 品牌
					bean.setPosName(pos.getPosName()); // 名称
					bean.setPosType(pos.getPosType()); // 机型
					
					bean.setDtCreate(new Date()); // 创建时间
					bean.setDtLastmod(new Date()); // 更新时间

					// 同一个品牌,同一个sn只能有一个
					YjPosBatchInfo tempObj = terminalSnBatchDao.queryYjPosBetchInfo(Utils.changeCharUpperCase(yjPosInfo.getSerialNum()),pos.getPosBrand());
					if(tempObj != null){//已经存在记录
						YjPosInfo temp=new YjPosInfo();
						temp.setSerialNum(tempObj.getSerialNum());

						String brandCode = tempObj.getPosBrand();
						String typeCode = tempObj.getPosType();
						String nameCode = tempObj.getPosName();
						
						temp.setPosBrand(ConfigMerinfoUtil.getPosBRAND(brandCode));
						temp.setPosName(ConfigMerinfoUtil.getPosMODEL(nameCode , brandCode));
						temp.setPosType(ConfigMerinfoUtil.getTerminalType(typeCode));
						
						importedList.add(temp);
						logger.info("##########操作员【"+operators.getAccountNo()+"】导入序列号【"+bean.getSerialNum() + "-" + pos.getPosBrand() + "】给代理商【"+operators.getAccountNo()+"】时重复，不处理##########");
						
						throw new ServiceException("终端序列号号段导入重复,重复的SN号为[" + bean.getSerialNum() + "]");
					} else {
						//不存在记录
						 baseDao.editSave(bean);
					}
					
			   }
			}
			 baseDao.addSyslog(SysLogType.SN_BATCH_IMPUT.getCode(),SysLogType.SN_BATCH_IMPUT.getName(),operators.getAccountNo(),"操作员【"+operators.getAccountNo()+"】成功导入【"+posinfo.size()+"】个终端号给代理商【"+pos.getAgentId()+"】,号段为[" + beginSn + "-" + endSn + "],厂商为[" + pos.getPosBrand() + "]");

			 logger.info("##########操作员【"+operators.getAccountNo()+"】成功导入【"+posinfo.size()+"】个终端号给代理商【"+pos.getAgentId()+"】,号段为[" + beginSn + "-" + endSn + "],厂商为[" + pos.getPosBrand() + "]##########");
				
		} catch (DaoException e) {
			logger.error(e.getMessage(),e);
			throw new ServiceException("终端序列号号段导入失败");
		}
		return importedList;
	}

	@Override
	@Transactional(rollbackFor = { ServiceException.class })
	public boolean batchTxtSaveSeriaNumInfo(YjPosBatch batchBean, Operators operators)
			throws ServiceException {
		boolean flag = false;
		YjPosBatchInfo bean = new YjPosBatchInfo();

		try {
			String beginSn = Utils.changeCharUpperCase( batchBean.getBeginSerialNum() );
			String endSn = Utils.changeCharUpperCase( batchBean.getEndSerialNum() );
			int nums = batchBean.getNums();
			
			// 生成批次号
			String snBatchNo = Constance.getSnBatchNo();
			// 插入批次表
			batchBean.setBeginSerialNum(beginSn); // 开始SN号--->转大写
			batchBean.setEndSerialNum(endSn); // 结束SN号--->转大写
			batchBean.setBatchNo(snBatchNo); // 批次号(日期 + _ + 随机数)
			batchBean.setOperatorNo(operators.getAccountNo());; // 操作账号
			batchBean.setOperatorId(operators.getId()); // 操作id
			batchBean.setDtCreate(new Date()); // 创建时间
			batchBean.setDtLastmod(new Date()); // 更新时间
			
			baseDao.editSave(batchBean);

			// 取SN最后一个字符的位置,便于分割
			int bLastChar = Constance.getSNLastCharPlace(beginSn);

			String bChars = getChar(bLastChar, beginSn);
			String bNumber = getSnNumber(bLastChar, beginSn);
			Long number = -1l;
			if( null != bNumber && !"".equals( bNumber )) {
				number = Long.parseLong( bNumber );
			}
			int zeroLength = beginSn.length() - bChars.length() - String.valueOf( number ).length();
			for ( int s = 1; s <= nums; s++) {
				bean=new YjPosBatchInfo();
				Long lastNumber = number - 1;
				
				if( String.valueOf( number ).length() > String.valueOf( lastNumber ).length() ) {
					zeroLength = zeroLength - 1;
				}
				String serialNum = bChars ;
				if ( number != -1l ) {
					serialNum = serialNum + Utils.addLeftZero(zeroLength, number);
				}
				bean.setBatchNo(snBatchNo);; // 批次号(日期 + _ + 随机数)
				bean.setSerialNum(Utils.changeCharUpperCase( serialNum ));// 终端sn序列号
				bean.setPosBrand(batchBean.getPosBrand()); // 品牌
				bean.setPosName(batchBean.getPosName()); // 名称
				bean.setPosType(batchBean.getPosType()); // 机型
				
				bean.setDtCreate(new Date()); // 创建时间
				bean.setDtLastmod(new Date()); // 更新时间

				// 同一个品牌,同一个sn只能有一个
				YjPosBatchInfo tempObj = terminalSnBatchDao.queryYjPosBetchInfo(Utils.changeCharUpperCase( serialNum ),batchBean.getPosBrand());
				if(tempObj != null){//已经存在记录
				
					logger.info("##########操作员【"+operators.getAccountNo()+"】导入序列号【"+bean.getSerialNum() + "-" + batchBean.getPosBrand() + "】给代理商【"+operators.getAccountNo()+"】时重复，不处理##########");
					
					throw new ServiceException("终端序列号号段导入重复,重复的SN号为[" + bean.getSerialNum() + "]");
				} else {
					//不存在记录
					 baseDao.editSave(bean);
				}
				number++;
			}
			logger.info("操作员【" + operators.getAccountNo() + "】添加了SN号段批次【" + snBatchNo  + "】,序列号段为【" + batchBean.getBeginSerialNum() + "-" + batchBean.getEndSerialNum() + "," + batchBean.getPosBrand() + "】");
			terminalSnBatchDao.addSyslog(SysLogType.SN_TXT_BATCH_INPUT.getCode(),
					SysLogType.SN_TXT_BATCH_INPUT.getName(), operators.getAccountNo(), "操作员【"
							+ operators.getAccountNo() + "】添加了SN号段批次【" + snBatchNo + "】,序列号段为【" + batchBean.getBeginSerialNum() + "-" + batchBean.getEndSerialNum() + "," + batchBean.getPosBrand() + "】");
			flag = true;
		} catch (DaoException e) {
			logger.error("######SN文本框号段添加失败######", e);
			throw new ServiceException("SN文本框号段添加失败!");
		} 
		
		return flag;
	}


	@Override
	@Transactional(rollbackFor = { ServiceException.class })
	public boolean batchYsbTxtSaveSeriaNumInfo(YjPosBatch batchBean, Operators operators)
			throws ServiceException {
		boolean flag = false;
		TPosInfo bean = new TPosInfo();

		try {
			String beginSn = Utils.changeCharUpperCase( batchBean.getBeginSerialNum() );
			String endSn = Utils.changeCharUpperCase( batchBean.getEndSerialNum() );
			int nums = batchBean.getNums();

			// 取SN最后一个字符的位置,便于分割
			int bLastChar = Constance.getSNLastCharPlace(beginSn);

			String bChars = getChar(bLastChar, beginSn);
			String bNumber = getSnNumber(bLastChar, beginSn);
			Long number = -1l;
			if( null != bNumber && !"".equals( bNumber )) {
				number = Long.parseLong( bNumber );
			}
			int zeroLength = beginSn.length() - bChars.length() - String.valueOf( number ).length();
			for ( int s = 1; s <= nums; s++) {
				Long lastNumber = number - 1;
				
				if( String.valueOf( number ).length() > String.valueOf( lastNumber ).length() ) {
					zeroLength = zeroLength - 1;
				}
				String serialNum = bChars ;
				if ( number != -1l ) {
					serialNum = serialNum + Utils.addLeftZero(zeroLength, number);
				}
//添加
				bean = new TPosInfo();
			
				bean.setSerialNum(Utils.changeCharUpperCase( serialNum ));
				bean.setMercCode(Constant.MERC_CODE_DEFAULT);
				bean.setTermCode(Constant.TERM_CODE_DEFAULT);
				bean.setIsBind(Terminal.BIND_TWO.getCode());//修改成未绑定
				bean.setOrgCode(batchBean.getOrgCode());
				bean.setPosBrand(batchBean.getPosBrand());
				bean.setPosName(batchBean.getPosName());
				bean.setPosType(batchBean.getPosType());
				bean.setOperators(operators.getAccountNo());
				bean.setUpdateFlag("N");
				bean.setAssefFlag( "0" ); // 公司直发
				bean.setRemark("由操作员【"+operators.getAccountNo() +"】导入云商宝序列号【"+bean.getSerialNum()+ "-" + bean.getPosBrand() + "】");

				bean.setTekId(Terminal.BIND_ONE.getCode());// 现存 有效状态，默认Y有效
				// 同一个品牌,同一个sn只能有一个
				TPosInfo tempObj = terminalDao.queryYsbYjPosInfo(bean.getSerialNum(),bean.getPosBrand());
				if(tempObj != null){//已经存在记录
				
					logger.info("##########操作员【"+operators.getAccountNo()+"】导入序列号【"+bean.getSerialNum() + "-" + batchBean.getPosBrand() + "】给代理商【"+operators.getAccountNo()+"】时重复，不处理##########");
					
					throw new ServiceException("终端序列号号段导入重复,重复的SN号为[" + bean.getSerialNum() + "]");
				}else{//不存在记录
					baseDao.editSave(bean);
					List<Object> params=new ArrayList<Object>();
					params.add(bean.getSerialNum());
					params.add(operators.getId());	
					params.add("N");	
					params.add(operators.getAccountNo());
					params.add("由操作员【"+operators.getAccountNo()+"】导入云商宝序列号【"+bean.getSerialNum()+ "-" + bean.getPosBrand() + "】给代理商【"+operators.getAccountNo()+"】");
					params.add(bean.getPosBrand());
					params.add("Y");	
					sqlBaseDao.deleteBySql("insert into t_pos_agent(id,serial_num,agent_id,is_used,operators,dt_create, dt_lastmod, remark,pos_brand,ZS_FLAG) values(seq_t_pos_agent.nextval,?,?,?,?,systimestamp,systimestamp,?,?,?)", params);
					
					logger.info("##########操作员【"+operators.getAccountNo()+"】成功导入云商宝序列号【"+bean.getSerialNum() + "-" + bean.getPosBrand() + "】给代理商【"+operators.getAccountNo()+"】##########");
						
				}
				number++;
				//添加结束
			}
			logger.info("操作员【" + operators.getAccountNo() + "】添加了云商宝SN号段批次,序列号段为【" + batchBean.getBeginSerialNum() + "-" + batchBean.getEndSerialNum() + "," + batchBean.getPosBrand() + "】");
			terminalSnBatchDao.addSyslog(SysLogType.YSB_SN_TXT_BATCH_INPUT.getCode(),
					SysLogType.YSB_SN_TXT_BATCH_INPUT.getName(), operators.getAccountNo(), "操作员【"
							+ operators.getAccountNo() + "】添加了云商宝SN号段,序列号段为【" + batchBean.getBeginSerialNum() + "-" + batchBean.getEndSerialNum() + "," + batchBean.getPosBrand() + "】");
			flag = true;
		} catch (DaoException e) {
			logger.error("######云商宝SN文本框号段添加失败######", e);
			throw new ServiceException("云商宝SN文本框号段添加失败!");
		} 
		
		return flag;
	}
	
	@Override
	@Transactional(rollbackFor = { ServiceException.class })
	public List<String> batchYsbSaveSeriaNumAgentInfo(YjPosBatch batchBean, Operators op, Operators toOperators,String flag)
			throws ServiceException {
		TPosInfo bean = new TPosInfo();
		TPosAgent tPosAgent = new TPosAgent();
		List<String> existList = new ArrayList<String>();
		try {
			String beginSn = Utils.changeCharUpperCase( batchBean.getBeginSerialNum() );
			String endSn = Utils.changeCharUpperCase( batchBean.getEndSerialNum() );
			int nums = batchBean.getNums();

			// 取SN最后一个字符的位置,便于分割
			int bLastChar = Constance.getSNLastCharPlace(beginSn);

			String bChars = getChar(bLastChar, beginSn);
			String bNumber = getSnNumber(bLastChar, beginSn);
			Long number = -1l;
			if( null != bNumber && !"".equals( bNumber )) {
				number = Long.parseLong( bNumber );
			}
			int zeroLength = beginSn.length() - bChars.length() - String.valueOf( number ).length();
			for ( int s = 1; s <= nums; s++) {
				Long lastNumber = number - 1;
				
				if( String.valueOf( number ).length() > String.valueOf( lastNumber ).length() ) {
					zeroLength = zeroLength - 1;
				}
				String serialNum = bChars ;
				if ( number != -1l ) {
					serialNum = serialNum + Utils.addLeftZero(zeroLength, number);
				}
                //分配SN  
				List<Object> list = null;
				tPosAgent = new TPosAgent();
				list = terminalDao.getYsbTermapplyList("N",Utils.changeCharUpperCase(serialNum),op.getId(),batchBean.getPosBrand(),"","",op.getRoleType());
                //是否未分配
				if (list != null && list.size() > 0) {
				
					String context = "操作员:" + op.getAccountNo() + "将机器序列号["+ serialNum + "-" + batchBean.getPosBrand() + "由[" + op.getAccountNo()+ "]通过号段方式分配给[" + toOperators.getAccountNo() + "]";
					String sql=null;				
					if(op.getRoleType().equals("14")){
						sql = "update  t_pos_agent  y set y.IS_USED=?,y.OPERATORS=?,y.DT_LASTMOD=sysdate,y.REMARK=? , y.flag=?,y.zs_flag='N' where y.SERIAL_NUM=? and y.AGENT_ID = ?  and y.POS_BRAND = ? "
								+" and exists ( select 1 from t_pos_info a where a.SERIAL_NUM = y.SERIAL_NUM and a.POS_BRAND = y.POS_BRAND and a.tek_id = 'Y') ";
                      }else{
                    	sql = "update  t_pos_agent  y set y.IS_USED=?,y.OPERATORS=?,y.DT_LASTMOD=sysdate,y.REMARK=?,y.zs_flag='N'  where y.SERIAL_NUM=? and y.AGENT_ID = ?  and y.POS_BRAND = ? "
  								+" and exists ( select 1 from t_pos_info a where a.SERIAL_NUM = y.SERIAL_NUM and a.POS_BRAND = y.POS_BRAND and a.tek_id = 'Y') ";
                       }
					List<Object> paramList = new ArrayList<Object>();
					paramList.add("Y");
					paramList.add(op.getAccountNo());
					paramList.add(context);
					if(op.getRoleType().equals("14")){
					paramList.add(flag);
					}
					paramList.add(Utils.changeCharUpperCase(serialNum));
					paramList.add(op.getId());
					paramList.add(batchBean.getPosBrand());
					int count = sqlBaseDao.deleteBySql(sql, paramList);// 修改
					if( count > 0 ) {
						String newsql="insert into t_pos_agent"
							      +"(id,serial_num,agent_id,is_used,operators,dt_create, dt_lastmod, remark,pos_brand,flag,zs_flag)"
							      +"values(seq_t_pos_agent.nextval, ?, ?, ?, ?, sysdate, sysdate, ?, ?,?,?)";
						//如果是仓库管理员分配找到直属上级
						if(op.getRoleType().equals("14")){
						  String upsql="select to_char(o.id),o.account_no from T_operators o where o.org_code ="+toOperators.getOrgCode()+"  and o.id<>"+toOperators.getId()+" AND o.ROLE_TYPE IN (02, 03)"+
						               "START WITH id ="+toOperators.getId()+" CONNECT BY PRIOR supri_id = id order by o.id";
						  List<Object> ob= sqlBaseDao.queryAll(upsql);
						  if (ob != null && ob.size() > 0) {
								Iterator iterator = ob.iterator();
								while (iterator.hasNext()) {
									    Object[] obj = (Object[]) iterator.next();
									    List<Object> inparamList= new ArrayList<Object>();
									    inparamList.add(Utils.changeCharUpperCase(serialNum));
									    inparamList.add(Long.valueOf(obj[0].toString()));
									    inparamList.add("Y");
									    inparamList.add(op.getAccountNo());
									    inparamList.add(context);
									    inparamList.add(batchBean.getPosBrand());
									    inparamList.add(flag);
									    inparamList.add("N");
									    int cnt= sqlBaseDao.deleteBySql(newsql, inparamList);//插入
									    if(cnt>0){
											baseDao.addSyslog(SysLogType.YSB_TER_DIS.getCode(),SysLogType.YSB_TER_DIS.getName(),  op.getAccountNo(),context+"号段分配");
										 }
										}
								}
						    List<Object> inparamList= new ArrayList<Object>();
						    inparamList.add(Utils.changeCharUpperCase(serialNum));
						    inparamList.add(Long.valueOf(toOperators.getId()));
						    inparamList.add("N");
						    inparamList.add(op.getAccountNo());
						    inparamList.add(context);
						    inparamList.add(batchBean.getPosBrand());
						    inparamList.add(flag);
						    inparamList.add("Y");
						    int cnt= sqlBaseDao.deleteBySql(newsql, inparamList);//插入
						    if(cnt>0){
								baseDao.addSyslog(SysLogType.YSB_TER_DIS.getCode(),SysLogType.YSB_TER_DIS.getName(),  op.getAccountNo(),context+"号段分配");
							 }
						   }else{
						   String newsql1="insert into t_pos_agent"
								      +"(id,serial_num,agent_id,is_used,operators,dt_create, dt_lastmod, remark,pos_brand,flag,zs_flag)"
								      +"values(seq_t_pos_agent.nextval, ?, ?, ?, ?, sysdate, sysdate, ?, ?,(select t.flag from apsmgm.t_pos_agent t where  t.serial_num =? and t.pos_brand=? and rownum = 1),'Y')";

						    List<Object> inparamList= new ArrayList<Object>();
						    inparamList.add(Utils.changeCharUpperCase(serialNum));
						    inparamList.add(Long.valueOf(toOperators.getId()));
						    inparamList.add("N");
						    inparamList.add(op.getAccountNo());
						    inparamList.add(context);
						    inparamList.add(batchBean.getPosBrand());
						    inparamList.add(Utils.changeCharUpperCase(serialNum));
						    inparamList.add(batchBean.getPosBrand());
						    int cnt= sqlBaseDao.deleteBySql(newsql1, inparamList);//插入
						    if(cnt>0){
								baseDao.addSyslog(SysLogType.YSB_TER_DIS.getCode(),SysLogType.YSB_TER_DIS.getName(),  op.getAccountNo(),context+"号段分配");
							 }	
						  }
						
						logger.info("成功将终端序列号【"+serialNum+ "-" + batchBean.getPosBrand() + "】分配给代理商【"+toOperators.getAccountNo()+"】");
					    }
					} else {
						existList.add(serialNum);
						logger.error("终端序列号【"+serialNum+ "-" + batchBean.getPosBrand() + "】已分配给其他代理商或者已出库或SN无效,不处理");
					}
				number++;
				//添加结束
			}
			logger.info("操作员【" + op.getAccountNo() + "】分配了云商宝SN号段批次,序列号段为【" + batchBean.getBeginSerialNum() + "-" + batchBean.getEndSerialNum() + "," + batchBean.getPosBrand() + "】");
			terminalSnBatchDao.addSyslog(SysLogType.YSB_SN_BATCH_ALLOT.getCode(),
					SysLogType.YSB_SN_BATCH_ALLOT.getName(), op.getAccountNo(), "操作员【"
							+ op.getAccountNo() + "】分配了云商宝SN号段,序列号段为【" + batchBean.getBeginSerialNum() + "-" + batchBean.getEndSerialNum() + "," + batchBean.getPosBrand() + "】");
		} catch (DaoException e) {
			logger.error("######云商宝SN号段分配失败######", e);
			throw new ServiceException("云商宝SN号段分配失败!");
		} 
		
		return existList;
	}
	
	private String getChar( int lastChar ,String sn ){
		String bChars = "";
		if( lastChar >= 0){
			bChars = sn.substring(0, lastChar + 1);
		}
		return bChars;
	}

	private String getSnNumber( int lastChar ,String sn ){
		String bNumber = "";
		if ( lastChar < ( sn.length()-1 ) ){
			bNumber = sn.substring( ( lastChar== -1 ? 0 : lastChar + 1 ), sn.length() );
		}
		return bNumber;
	}
	
	
	/**
	 * 删除SN号段信息
	 * 
	 * @param posBatch
	 * @param oper
	 * @return
	 * @throws ServiceException
	 */
	@Override
	@Transactional(rollbackFor = { ServiceException.class })
	public boolean deleteTerminalSNBatch(YjPosBatch posBatch, Operators oper) throws ServiceException {
		boolean flag = false;
		try {
			String accountNo = oper.getAccountNo();
			String batchNo = posBatch.getBatchNo();
			
			// 删除 SN 批次信息
			terminalSnBatchDao.deletePOSBatchInfoByBatchNo(batchNo);
			// 删除 SN 批次
			terminalSnBatchDao.deletePOSBatchByBatchNo(batchNo);

			logger.info("操作员【" + accountNo + "】删除了SN号段批次【" + batchNo + "】");
			terminalSnBatchDao.addSyslog(SysLogType.SN_BATCH_DELETE.getCode(),
					SysLogType.SN_BATCH_DELETE.getName(), accountNo, "操作员【"
							+ accountNo + "】删除了SN号段批次【" + batchNo + "】");
			flag = true;
		} catch (DaoException e) {
			logger.error("######SN号段删除失败######", e);
			throw new ServiceException("SN号段删除失败!");
		} 

		return flag;
	
	}
	
}
